from plotly.graph_objs import Bar,Layout
from plotly import offline

from random import randint

class Die:
    '''表示一个骰子的类'''

    def __init__(self,num_sides=6):
        '''骰子默认为6面'''
        self.num_sides=num_sides
    
    def roll(self):
        '''返回一个位于1和骰子面数之间的随机值'''
        return randint(1,self.num_sides)
    
#创建3个D6
die_1=Die()
die_2=Die()
die_3=Die()

#掷几个骰子并将结果存储在一个列表中
results=[]
for roll_num in range(500_000):
    result=die_1.roll()+die_2.roll()+die_3.roll()
    results.append(result)

#分析结果
frequencies=[]
max_result=die_1.num_sides*3
for value in range(3,max_result+1):
    frequency=results.count(value)
    frequencies.append(frequency)

#可视化结果
x_values=list(range(3,max_result+1))
data=[Bar(x=x_values,y=frequencies)]

x_axis_config={'title':'结果','dtick':1}
y_axis_config={'title':'结果的频率'}
my_layout=Layout(title='掷三次D6 50000次的结果',
                 xaxis=x_axis_config,yaxis=y_axis_config)
offline.plot({'data':data,'layout':my_layout},filename='d6_d6_d6.html')